import json
from py2neo import *
from django.shortcuts import render

# 连接数据库
graph = Graph('http://localhost:7474', user='neo4j', password='yhf20001116', name='neo4j')


def search_all():

    data = []
    links = []
    result = graph.run("MATCH (n) RETURN n")
    for n in graph.nodes:
        print(n.labels)
        nodesStr = json.dumps(graph.nodes[n], ensure_ascii=False)
        node_name = json.loads(nodesStr)['name']
        dict = {
            'name': node_name,
            'symbolSize': 50,
            'category': n.labels
        }
        data.append(dict)
    rps = graph.relationships
    for r in rps:
        # 取出开始节点的name
        source = str(rps[r].start_node['name'])
        # 取出结束节点的name
        target = str(rps[r].end_node['name'])
        # 取出开始节点的结束节点之间的关系
        name = str(type(rps[r]).__name__)
        # 构造字典存储单个关系信息
        dict = {
            'source': source,
            'target': target,
            'type': name
        }
        links.append(dict)

    # for item in data:
    #     print(item)
    # 将所有的节点信息和关系信息存放在一个字典中
    neo4j_data = {
        'data': data,
        'links': links
    }
    #将字典转化json格式
    neo4j_data = json.dumps(neo4j_data, ensure_ascii=False)
    return neo4j_data

if __name__ == '__main__':
    print(123)
    neo4j_data = search_all()
    print(neo4j_data)